package com.sl.core.engine.meta.row;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sl.core.domain.dto.JsonFieldDefine;
import com.sl.core.utils.JsonUtil;
import com.sl.core.engine.factory.ProcessConfigFactory;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.trans.ProcessTransDescriptor;
import com.sl.core.engine.meta.valuemeta.ObjArrayMetaInterface;
import com.sl.core.engine.meta.valuemeta.ValueMetaArray;
import com.sl.core.engine.meta.valuemeta.ValueMetaObject;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.IValueMeta;
import org.apache.hop.core.row.RowMeta;
import org.apache.hop.core.row.value.*;
import org.apache.hop.core.variables.Variables;
import org.apache.hop.pipeline.transform.ITransformMeta;
import org.apache.hop.pipeline.transform.TransformMeta;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
public class ProcessRowDescriptor extends RowMeta {

    private Boolean isArray;


    private Boolean basicTypeFlag;

    private String basicFieldType;

    private static Map<Integer, String> VALUE_META_MAPPING = Maps.newConcurrentMap();

    static {
        VALUE_META_MAPPING.put(IValueMeta.TYPE_STRING, "String");
        VALUE_META_MAPPING.put(IValueMeta.TYPE_INTEGER, "Integer");
        VALUE_META_MAPPING.put(IValueMeta.TYPE_BIGNUMBER, "BigNumber");
        VALUE_META_MAPPING.put(IValueMeta.TYPE_TIMESTAMP, "Timestamp");
        VALUE_META_MAPPING.put(IValueMeta.TYPE_BOOLEAN, "Boolean");
        VALUE_META_MAPPING.put(IValueMeta.TYPE_NUMBER, "Number");

        VALUE_META_MAPPING.put(Integer.valueOf(ValueMetaObject.TYPE), "Object");


    }

    public ProcessRowDescriptor() {

    }

    public Boolean getIsArray() {
        return ObjectUtil.defaultIfNull(isArray, false);
    }

    public ProcessRowDescriptor(IRowMeta queryFields) {
        this.addRowMeta(queryFields);
    }

    public ProcessRowDescriptor(List<IValueMeta> iValueMetas) {
        if (CollUtil.isEmpty(iValueMetas)) {
            return;
        }
        for (IValueMeta iValueMeta : iValueMetas) {
            this.addValueMeta(iValueMeta);
        }
    }

    public static String defaultValueType() {
        return VALUE_META_MAPPING.get(IValueMeta.TYPE_STRING);
    }

    public void addValueMeta(List<IValueMeta> valueMetaList) {
        if (CollUtil.isEmpty(valueMetaList)) {
            return;
        }
        for (IValueMeta iValueMeta : valueMetaList) {
            this.addValueMeta(iValueMeta);
        }
    }

    public static String defaultValueType(String basicFieldType) {
        String s = VALUE_META_MAPPING.get(IValueMeta.TYPE_STRING);
        if (StrUtil.isBlank(basicFieldType)) {
            return s;
        }
        return basicFieldType;
    }


    @Override
    public void addRowMeta(IRowMeta rowMeta) {
        super.addRowMeta(rowMeta);
        if (rowMeta instanceof ProcessRowDescriptor processRowDescriptor) {
            this.setIsArray(processRowDescriptor.getIsArray());
            this.setBasicTypeFlag(processRowDescriptor.getBasicTypeFlag());
            this.setBasicFieldType(processRowDescriptor.getBasicFieldType());
        }
    }

    public Boolean isBasicTypeFlag() {
        return BooleanUtil.isTrue(basicTypeFlag);
    }

    public boolean isEmptyAndNotBasic() {
        boolean empty = super.isEmpty();
        return empty && !isBasicTypeFlag();
    }

    @Override
    public void clear() {
        super.clear();
    }

    public void addObj(String name) {
        ValueMetaObject valueMetaObject = new ValueMetaObject();
        valueMetaObject.setName(name);
        this.addValueMeta(valueMetaObject);
    }


    public void addString(String name) {
        ValueMetaString valueString = new ValueMetaString();
        valueString.setName(name);
        this.addValueMeta(valueString);
    }

    public void addListString(String name) {
        ValueMetaArray valueMetaArray = new ValueMetaArray();
        valueMetaArray.setName(name);
        ValueMetaString valueString = new ValueMetaString();
        valueMetaArray.setFieldType(valueString);
        this.addValueMeta(valueMetaArray);
    }

    public void addFieldDefine(List<JsonFieldDefine> defineList) {
        if (CollUtil.isEmpty(defineList)) {
            return;
        }
        defineList.forEach(e -> {
            IValueMeta valueMetaInterface = convertMetaInterface(e);
            this.addValueMeta(valueMetaInterface);
        });
    }

    public static String typeToFieldTypeCode(Integer type) {
        return VALUE_META_MAPPING.get(type);
    }

    public static Integer outFieldTypeTransfer(String filedTypeCode) {
        Integer type = null;
        if (StrUtil.isBlank(filedTypeCode)) {
            filedTypeCode = VALUE_META_MAPPING.get(IValueMeta.TYPE_STRING);
        }
        for (Map.Entry<Integer, String> stringEntry : VALUE_META_MAPPING.entrySet()) {
            String value = stringEntry.getValue();
            if (StrUtil.equalsAnyIgnoreCase(value, filedTypeCode)) {
                type = stringEntry.getKey();
                break;
            }
        }
        return type;
    }

    @SneakyThrows
    public IValueMeta convertMetaInterface(JsonFieldDefine fieldDefine) {
//        ValueMetaInterface valueMetaInterface = null;
        String type = fieldDefine.getFieldType();
        if (StrUtil.isBlank(type)) {
            fieldDefine.setFieldType(VALUE_META_MAPPING.get(IValueMeta.TYPE_STRING));
        }
        Integer i = outFieldTypeTransfer(type);
        if (ObjectUtil.isNull(i)) {
            i = IValueMeta.TYPE_STRING;
        }
        IValueMeta valueMeta = ValueMetaFactory.createValueMeta(i);

        //如果是数组
        if (BooleanUtil.isTrue(fieldDefine.getIsArrayFlag())) {
            //转为数组
            ValueMetaArray array = (ValueMetaArray) ValueMetaFactory.createValueMeta(Integer.parseInt(ValueMetaArray.TYPE));
            // 设置数组里存放的对象类型
            array.setFieldType(valueMeta);
            List<JsonFieldDefine> childrenList = fieldDefine.getChildren();
            array.setFields(convertMetaInterface(childrenList));
            valueMeta = array;
        } else if (StrUtil.equals(i + "", ValueMetaObject.TYPE)) {
            ValueMetaObject obj = (ValueMetaObject) valueMeta;
            List<JsonFieldDefine> children = fieldDefine.getChildren();
            List<IValueMeta> valueMetaInterfaces = convertMetaInterface(children);
            obj.setFields(valueMetaInterfaces);
        }
        valueMeta.setName(fieldDefine.getFieldCode());
        valueMeta.setComments(fieldDefine.getFieldDesc());
        return valueMeta;
    }

    public List<IValueMeta> convertMetaInterface(List<JsonFieldDefine> defineList) {
        if (CollUtil.isEmpty(defineList)) {
            return Collections.emptyList();
        }
        ArrayList<IValueMeta> arr = new ArrayList<>();
        defineList.forEach(e -> arr.add(convertMetaInterface(e)));
        return arr;
    }


    @SneakyThrows
    public IValueMeta objToValueMetaInterface(String k, Object v) {
        IValueMeta valueMeta = null;
        if (v == null) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_NONE);
            valueMeta.setName(k);
        } else if (v instanceof String) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_STRING);
            valueMeta.setName(k);
        } else if (v instanceof Long) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_BIGNUMBER);
            valueMeta.setName(k);
        } else if (v instanceof Integer) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_INTEGER);
            valueMeta.setName(k);
        } else if (v instanceof Date) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_DATE);
            valueMeta.setName(k);
        } else if (v instanceof Boolean) {
            valueMeta = ValueMetaFactory.createValueMeta(IValueMeta.TYPE_BOOLEAN);
            valueMeta.setName(k);
        } else if (v instanceof Map) {
            valueMeta = ValueMetaFactory.createValueMeta(Integer.parseInt(ValueMetaObject.TYPE));
            valueMeta.setName(k);
            List<IValueMeta> valueMetaInterfaces = convertMetaInterface((Map<String, ?>) v);
            if (CollUtil.isNotEmpty(valueMetaInterfaces)) {
                ((ValueMetaObject) valueMeta).setFields(valueMetaInterfaces);
            }
        } else if (v instanceof Collection<?>) {
            Collection f = (Collection) v;
            valueMeta = ValueMetaFactory.createValueMeta(Integer.parseInt(ValueMetaArray.TYPE));
            valueMeta.setName(k);

            if (CollUtil.isNotEmpty(f)) {
                //取第一条来说
                Object o = CollUtil.get(f, 0);
                if (o instanceof Map) {
                    Map fm = (Map) o;
                    ((ValueMetaArray) valueMeta).setFields(convertMetaInterface(fm));
                    ((ValueMetaArray) valueMeta).setFieldType(ValueMetaFactory.createValueMeta(Integer.valueOf(ValueMetaObject.TYPE)));
                } else {
                    ((ValueMetaArray) valueMeta).setFieldType(objToValueMetaInterface(k, o));
                }
            } else {
                ((ValueMetaArray) valueMeta).setFieldType(objToValueMetaInterface(k, null));
            }

        }
        return valueMeta;
    }

    public void mergeJsonDefine(List<JsonFieldDefine> jsonFieldDefines) {
        List<IValueMeta> valueMetaList = this.getValueMetaList();
        mergeJsonDefine("$", valueMetaList, jsonFieldDefines);
    }

    public void mergeJsonDefine(String path, List<IValueMeta> valueMetaList, List<JsonFieldDefine> jsonFieldDefines) {
        if (CollUtil.isEmpty(valueMetaList)) {
            return;
        }
        for (IValueMeta iValueMeta : valueMetaList) {
            String name = iValueMeta.getName();
            String ps = path + "." + name;

            JsonFieldDefine jsonFieldDefine = findJsonFieldDefine(ps, jsonFieldDefines);
            if (ObjectUtil.isNotNull(jsonFieldDefine)) {
                setDefaultValue(iValueMeta, jsonFieldDefine.getDefaultValue());
            }
            if (iValueMeta instanceof ValueMetaArray valueMetaArray) {

                List<IValueMeta> fields = valueMetaArray.getFields();
                mergeJsonDefine(ps, fields, jsonFieldDefines);
            } else if (iValueMeta instanceof ValueMetaObject valueMetaObject) {
                List<IValueMeta> fields = valueMetaObject.getFields();
                mergeJsonDefine(ps, fields, jsonFieldDefines);
            }
        }

    }

    public JsonFieldDefine findJsonFieldDefine(String path, List<JsonFieldDefine> jsonFieldDefines) {

        if (CollUtil.isEmpty(jsonFieldDefines)) {
            return null;
        }
        JsonFieldDefine js = null;
        for (JsonFieldDefine jsonFieldDefine : jsonFieldDefines) {
            String path1 = jsonFieldDefine.getPath();
            if (StrUtil.equalsAnyIgnoreCase(path, path1)) {
                js = jsonFieldDefine;
                break;
            }
            List children = jsonFieldDefine.getChildren();
            JsonFieldDefine jsonFieldDefine1 = findJsonFieldDefine(path, children);
            if (ObjectUtil.isNotNull(jsonFieldDefine1)) {
                js = jsonFieldDefine1;
                break;
            }
        }
        return js;
    }

    @SneakyThrows
    public ProcessRowDescriptor convertMetaI(Object o) {
        if (ObjectUtil.isNull(o)) {
            return null;
        }
        ProcessRowDescriptor processRowDescriptor = new ProcessRowDescriptor();
        List<IValueMeta> iValueMeta = convertMetaIO(o, true);

        if (o instanceof Collection<?>) {
            processRowDescriptor.setIsArray(true);
        }

        processRowDescriptor.addValueMeta(iValueMeta);
        if (CollUtil.size(iValueMeta) == 1) {
            IValueMeta iValueMeta1 = CollUtil.get(iValueMeta, 0);
            if (StrUtil.equals(iValueMeta1.getName(), "args0")) {
                processRowDescriptor.clear();
                processRowDescriptor.setBasicFieldType(VALUE_META_MAPPING.get(iValueMeta1.getType()));
                processRowDescriptor.setBasicTypeFlag(true);
            }
        }
        return processRowDescriptor;
    }

    public List<IValueMeta> convertMetaIO(Object o, Boolean root) {

        List<IValueMeta> resultList = Lists.newArrayList();
        if (ObjectUtil.isNull(o)) {
            return resultList;
        }
        if (o instanceof Map m) {

            Set<String> set = m.keySet();

            ValueMetaObject valueMetaObject = new ValueMetaObject();
            List<IValueMeta> valueMetaList = Lists.newArrayList();
            for (String object : set) {
                Object o1 = m.get(object);
                List<IValueMeta> iValueMeta = convertMetaIO(o1, false);
                for (IValueMeta valueMeta : iValueMeta) {
                    valueMeta.setName(object);
                }
                valueMetaList.addAll(iValueMeta);
            }

            if (BooleanUtil.isTrue(root)) {
                resultList.addAll(valueMetaList);
            } else {
                valueMetaObject.setFields(valueMetaList);
                resultList.add(valueMetaObject);
            }


        } else if (o instanceof Collection<?> cl) {
            Object o1 = CollUtil.get(cl, 0);

            List<IValueMeta> iValueMetas1 = convertMetaIO(o1, false);
            if (BooleanUtil.isTrue(root)) {
                iValueMetas1 = valueMetaResolve(iValueMetas1);
                resultList.addAll(iValueMetas1);
            } else {
                ValueMetaArray valueMetaArray = new ValueMetaArray();

                iValueMetas1 = valueMetaResolve(iValueMetas1);
                valueMetaArray.setFields(iValueMetas1);
                if (o1 instanceof Map) {
                    valueMetaArray.setFieldType(new ValueMetaObject());
                } else {
                    valueMetaArray.setFields(null);
                    valueMetaArray.setFieldType(CollUtil.get(iValueMetas1, 0));
                }
                resultList.add(valueMetaArray);
            }

        } else {
            IValueMeta iValueMeta = ProcessConfigFactory.guessValueMetaInterface(o);
            iValueMeta.setName("args0");
            resultList.add(iValueMeta);
        }
        return resultList;
    }


    private List<IValueMeta> valueMetaResolve(List<IValueMeta> iValueMetas1) {
        if (CollUtil.size(iValueMetas1) == 1) {
            IValueMeta iValueMeta1 = CollUtil.get(iValueMetas1, 0);

            if (iValueMeta1 instanceof ValueMetaObject valueMetaObject) {
                String name = iValueMeta1.getName();
                if (StrUtil.isBlank(name)) {
                    iValueMetas1 = valueMetaObject.getFields();
                }
            } else if (iValueMeta1 instanceof ValueMetaArray valueMetaArray) {
                String name = iValueMeta1.getName();
                List<IValueMeta> fields = valueMetaArray.getFields();
                IValueMeta fieldType = valueMetaArray.getFieldType();
                if (StrUtil.isBlank(name)) {
                    iValueMetas1 = fields;
                    if (CollUtil.isEmpty(fields)) {
                        iValueMetas1 = Lists.newArrayList(fieldType);
                    }
                }
            }
        }
        return iValueMetas1;
    }

    @SneakyThrows
    public List<IValueMeta> convertMetaInterface(Map<String, ?> json) {
        List<IValueMeta> res = Lists.newArrayList();
        for (Map.Entry<String, ?> entry : json.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            IValueMeta valueMetaInterface = objToValueMetaInterface(k, v);
            if (ObjectUtil.isNotNull(valueMetaInterface)) {
                res.add(valueMetaInterface);
            }
        }

        return res;
    }

    public Map<String, Object> toFieldDefine() {
        //现在开始转fieldDefine
        Map<String, Object> maps = Maps.newHashMap();
        maps.put("isArrayFlag", this.getIsArray());
        maps.put("basicTypeFlag", this.getBasicTypeFlag());
        maps.put("basicFieldType", this.getBasicFieldType());

        List<IValueMeta> valueMetaList = this.getValueMetaList();
        List<Map<String, Object>> fieldDefine = getFieldDefine(valueMetaList);
        maps.put("params", fieldDefine);
        return maps;
    }

    @SneakyThrows
    public Map<String, Object> toInputDefine(ProcessTransDescriptor transMeta, String stepCode) {

        //找当前组件是否存在input
        TransformMeta step = transMeta.findTransform(stepCode);
        ITransformMeta stepMetaInterface = step.getTransform();
        ProcessRowDescriptor inputRowDescriptor = null;
        if (stepMetaInterface instanceof AbstractProcessCompDescriptor descriptorService) {
            inputRowDescriptor = descriptorService.getInputRowDescriptor();
        }

        if (ObjectUtil.isNull(inputRowDescriptor)) {
            IRowMeta stepFields = transMeta.getPrevTransformFields(new Variables(), stepCode);
            inputRowDescriptor = new ProcessRowDescriptor();
            inputRowDescriptor.addRowMeta(stepFields);
        }

        this.addRowMeta(inputRowDescriptor);

        //现在开始转fieldDefine
        return this.toFieldDefine();
    }

    public void setDefaultValue(IValueMeta valueMetaInterface, String value) {
        setValue(valueMetaInterface, value, 0);
    }

    public void setValue(IValueMeta valueMetaInterface, String value, Integer index) {
        Object[] indexArray = valueMetaInterface.getIndex();

        if (ObjectUtil.isNull(indexArray)) {
            indexArray = new Object[]{};
        }
        List<Object> res = Arrays.stream(indexArray).collect(Collectors.toList());

        res.add(index, value);
        valueMetaInterface.setIndex(res.toArray());
    }

    public String getDefaultValue(IValueMeta valueMetaInterface) {
        Object[] index = valueMetaInterface.getIndex();

        if (ArrayUtil.isNotEmpty(index)) {

            Object o = ArrayUtil.get(index, 0);
            return StrUtil.str(o, Charset.defaultCharset());
        }
        return null;
    }

    private List<Map<String, Object>> getFieldDefine(List<IValueMeta> valueMetaList) {
        if (CollUtil.isEmpty(valueMetaList)) {
            return null;
        }
        List<Map<String, Object>> defines = Lists.newArrayList();
        for (IValueMeta valueMetaInterface : valueMetaList) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("fieldCode", valueMetaInterface.getName());
            map.put("fieldName", valueMetaInterface.getName());
            map.put("fieldType", VALUE_META_MAPPING.get(valueMetaInterface.getType()));
            map.put("fieldDesc", valueMetaInterface.getComments());
            map.put("defaultValue", getDefaultValue(valueMetaInterface));

            if (valueMetaInterface instanceof ValueMetaArray valueMetaArray) {
                IValueMeta fieldType = valueMetaArray.getFieldType();
                if (ObjectUtil.isNotNull(fieldType)) {
                    map.put("fieldType", VALUE_META_MAPPING.get(fieldType.getType()));
                }
                map.put("isArrayFlag", true);
            } else {
                map.put("isArrayFlag", false);
            }
            map.put("remark", valueMetaInterface.getComments());
//            map.put("sort",valueMetaInterface.getso());

            if (valueMetaInterface instanceof ValueMetaArray || valueMetaInterface instanceof ValueMetaObject) {
                List<IValueMeta> fields = ((ObjArrayMetaInterface) valueMetaInterface).getFields();
                map.put("children", getFieldDefine(fields));
            }

            defines.add(map);
        }
        return defines;
    }

    @SneakyThrows
    public Map<String, Object> toOutputDefine(ProcessTransDescriptor transMeta, String stepCode) {

        //找当前组件是否存在input
        TransformMeta step = transMeta.findTransform(stepCode);
        ITransformMeta stepMetaInterface = step.getTransform();
        ProcessRowDescriptor inputRowDescriptor = null;
        if (stepMetaInterface instanceof AbstractProcessCompDescriptor descriptorService) {
            inputRowDescriptor = descriptorService.getResultRowDescriptor();
        }

        if (ObjectUtil.isNull(inputRowDescriptor)) {
            IRowMeta stepFields = transMeta.getTransformFields(new Variables(), stepCode);
            inputRowDescriptor = new ProcessRowDescriptor();
            inputRowDescriptor.addRowMeta(stepFields);
        }

        this.addRowMeta(inputRowDescriptor);


        return this.toFieldDefine();
    }

    @Override
    public RowMeta clone() {
        RowMeta clone = super.clone();
        ProcessRowDescriptor rowDescriptor = new ProcessRowDescriptor(clone);
        rowDescriptor.setIsArray(this.isArray);
        rowDescriptor.setBasicFieldType(this.basicFieldType);
        rowDescriptor.setBasicTypeFlag(this.basicTypeFlag);
        return rowDescriptor;
    }

    public void copyField(IRowMeta add) {
        if (add instanceof ProcessRowDescriptor processRowDescriptor) {
            this.setIsArray(processRowDescriptor.getIsArray());
            this.setBasicTypeFlag(processRowDescriptor.getBasicTypeFlag());
            this.setBasicFieldType(processRowDescriptor.getBasicFieldType());
        }
    }

    public void toUpperCase() {
        toUpperCase(this.getValueMetaList());
    }

    public void toUpperCase(List<IValueMeta> valueMetaList) {
        toLowerCaseUnderCame(valueMetaList, true, false, false, false);
    }

    private void toLowerCaseUnderCame(List<IValueMeta> valueMetaList, Boolean upper, Boolean lower,
                                      Boolean underLine, Boolean cameCase) {
        if (CollUtil.isEmpty(valueMetaList)) {
            return;
        }
        for (IValueMeta valueMeta : valueMetaList) {
            String name = valueMeta.getName();
            if (BooleanUtil.isTrue(upper)) {
                valueMeta.setName(name.toUpperCase());
            }
            if (BooleanUtil.isTrue(lower)) {
                valueMeta.setName(name.toLowerCase());
            }
            if (BooleanUtil.isTrue(underLine)) {
                valueMeta.setName(StrUtil.toUnderlineCase(name));
            }
            if (BooleanUtil.isTrue(cameCase)) {
                valueMeta.setName(StrUtil.toCamelCase(name));
            }
            if (valueMeta instanceof ValueMetaArray valueMetaArray) {
                List<IValueMeta> fields = valueMetaArray.getFields();
                toLowerCaseUnderCame(fields, upper, lower, underLine, cameCase);
            } else if (valueMeta instanceof ValueMetaObject valueMetaObject) {
                List<IValueMeta> fields = valueMetaObject.getFields();
                toLowerCaseUnderCame(fields, upper, lower, underLine, cameCase);
            }
        }
    }

    public void toLowerCase() {
        toLowerCase(this.getValueMetaList());
    }

    public void toLowerCase(List<IValueMeta> valueMetaList) {
        toLowerCaseUnderCame(valueMetaList, false, true, false, false);
    }

    public void toUnderLine() {
        toUnderLine(this.getValueMetaList());
    }

    public void toUnderLine(List<IValueMeta> valueMetaList) {
        toLowerCaseUnderCame(valueMetaList, false, false, true, false);
    }

    public void toCameCase() {
        toCameCase(this.getValueMetaList());
    }

    public void toCameCase(List<IValueMeta> valueMetaList) {
        toLowerCaseUnderCame(valueMetaList, false, false, false, true);
    }

    public Object mockValue() {

        Boolean isArray = this.getIsArray();
        Boolean basicTypeFlag = this.getBasicTypeFlag();
        String basicFieldType = this.getBasicFieldType();
        Object result = null;
        if (BooleanUtil.isTrue(basicTypeFlag)) {
            result = fieldTypeToDefaultValue(defaultValueType(basicFieldType));
        } else {
            List<IValueMeta> valueMetaList = this.getValueMetaList();
            result = valueMetaMockValue(valueMetaList);
        }
        if (BooleanUtil.isTrue(isArray)) {
            result = Lists.newArrayList(result);
        }
        return result;
    }

    private Map<String, Object> valueMetaMockValue(List<IValueMeta> valueMetaList) {
        Map<String, Object> maps = Maps.newHashMap();

        for (IValueMeta valueMeta : valueMetaList) {
            String name = valueMeta.getName();

            int type = valueMeta.getType();

            if (valueMeta instanceof ValueMetaObject valueMetaObject) {
                List<IValueMeta> fields = valueMetaObject.getFields();
                Map<String, Object> map = valueMetaMockValue(fields);
                maps.put(name, map);
            } else if (valueMeta instanceof ValueMetaArray valueMetaArray) {
                List<IValueMeta> fields = valueMetaArray.getFields();
                IValueMeta fieldType = valueMetaArray.getFieldType();

                if (CollUtil.isNotEmpty(fields)) {
                    Map<String, Object> map = valueMetaMockValue(fields);
                    maps.put(name, Lists.newArrayList(map));
                } else {
                    Object o = fieldTypeToDefaultValue(fieldType.getType());
                    maps.put(name, Lists.newArrayList(o));
                }
            } else {
                Object o = fieldTypeToDefaultValue(type);
                maps.put(name, o);

            }
        }
        return maps;

    }

    @SneakyThrows
    private Object fieldTypeToDefaultValue(String basicFieldType) {

        Integer i = outFieldTypeTransfer(basicFieldType);
        return fieldTypeToDefaultValue(i);
    }

    @SneakyThrows
    private Object fieldTypeToDefaultValue(Integer type) {

        IValueMeta valueMeta = ValueMetaFactory.createValueMeta(type);
        if (valueMeta instanceof ValueMetaString) {
            return "";
        } else if (valueMeta instanceof ValueMetaInteger) {
            return 0;
        } else if (valueMeta instanceof ValueMetaBigNumber) {
            return BigDecimal.ZERO;
        } else if (valueMeta instanceof ValueMetaBoolean) {
            return false;
        } else if (valueMeta instanceof ValueMetaObject) {
//            Map<String, Object> map = Maps.newHashMap();
            return Maps.newHashMap();
//            Object o = fieldTypeToDefaultValue(VALUE_META_MAPPING.get(valueMeta.getType()));

//
//            map.put()
//
        } else if (valueMeta instanceof ValueMetaArray) {
//            Map<String, Object> map = Maps.newHashMap();
            return Lists.newArrayList();
//            Object o = fieldTypeToDefaultValue(VALUE_META_MAPPING.get(valueMeta.getType()));
//
//            map.put()
//
        }
        return null;
    }

    @SneakyThrows
    public List<Map<String, Object>> toInputDefineGroup(ProcessTransDescriptor transMeta, String stepCode) {

//
        List<Map<String, Object>> result = Lists.newArrayList();
        //找当前组件是否存在input
        TransformMeta step = transMeta.findTransform(stepCode);

        List<TransformMeta> previousTransforms = transMeta.findPreviousTransforms(step);

        for (TransformMeta previousTransform : previousTransforms) {
            Map<String, Object> map = Maps.newHashMap();

            ProcessRowDescriptor p = new ProcessRowDescriptor();
            Map<String, Object> outputDefine = p.toOutputDefine(transMeta, previousTransform.getName());
            outputDefine.put("isArrayFlag", true);

            map.put("fromTransformName", previousTransform.getName());
            map.put("outputDefine", outputDefine);
            result.add(map);
        }
        return result;
    }

    public String toStringMeta() {
        Map<String, Object> fieldDefine = this.toFieldDefine();
        return JsonUtil.toStr(fieldDefine);
    }

    public Map<String, Object> toProcessEventInputDefine(ProcessTransDescriptor transMeta) {

        List<TransformMeta> transforms = transMeta.getTransforms();

        List<TransformMeta> list = transforms.stream().filter(v -> {
            ITransformMeta transform = v.getTransform();
            if (!(transform instanceof AbstractProcessCompDescriptor processCompDescriptor)) {
                return false;
            }
            return BooleanUtil.isTrue(processCompDescriptor.getIsStartStep());
        }).toList();


        Map<String, Map<String, Object>> result = Maps.newHashMap();
        for (TransformMeta transformMeta : list) {
            String name = transformMeta.getName();
            Map<String, Object> inputDefine = toInputDefine(transMeta, name);
            result.put(name, inputDefine);
        }
        if (MapUtil.isEmpty(result)) {
            return Collections.emptyMap();
        }

        // if (result.size() == 1) {
        //默认只取第一条
        return result.get(CollUtil.get(result.keySet(), 0));
        //  }
    }


    public Map<String, Object> getProcessEventOutputDefine(ProcessTransDescriptor transMeta) {

        List<TransformMeta> transforms = transMeta.getTransforms();

        List<TransformMeta> list = transforms.stream().filter(v -> {
            ITransformMeta transform = v.getTransform();
            if (!(transform instanceof AbstractProcessCompDescriptor processCompDescriptor)) {
                return false;
            }
            return BooleanUtil.isTrue(processCompDescriptor.getIsEndStep());
        }).toList();


        Map<String, Map<String, Object>> result = Maps.newHashMap();
        for (TransformMeta transformMeta : list) {
            String name = transformMeta.getName();
            Map<String, Object> inputDefine = toOutputDefine(transMeta, name);
            result.put(name, inputDefine);
        }
        if (MapUtil.isEmpty(result)) {
            return Collections.emptyMap();
        }

        // if (result.size() == 1) {
        //默认只取第一条
        return result.get(CollUtil.get(result.keySet(), 0));
    }

    public static ProcessRowDescriptor objectToRowMeta(Object tr) {

        ProcessRowDescriptor outputMeta = new ProcessRowDescriptor();

        if (ObjectUtil.isNull(tr)) {
            return outputMeta;
        }
        List<IValueMeta> list = Lists.newArrayList();
        if (tr instanceof Collection) {
            int i = 0;
            for (Object v : ((Collection<?>) tr)) {

                if (v instanceof Map) {
                    outputMeta.setBasicTypeFlag(false);
//                        outputMeta.setBasicFieldType();
                    ((Map) v).forEach((kk, vv) -> {
                        IValueMeta valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface(vv);
                        if (ObjectUtil.isNull(valueMetaInterface)) {
                            valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface("");
                        }
                        if (valueMetaInterface instanceof ValueMetaArray) {
                            ProcessRowDescriptor openSkyRowMeta = objectToRowMeta(vv);
                            ((ValueMetaArray) valueMetaInterface).setFields(openSkyRowMeta.getValueMetaList());
                        } else if (valueMetaInterface instanceof ValueMetaObject) {
                            ProcessRowDescriptor openSkyRowMeta = objectToRowMeta(vv);
                            ((ValueMetaObject) valueMetaInterface).setFields(openSkyRowMeta.getValueMetaList());
                        }
                        valueMetaInterface.setName((String) kk);
                        list.add(valueMetaInterface);
                    });
                } else {
                    IValueMeta valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface(v);
                    if (ObjectUtil.isNull(valueMetaInterface)) {
                        valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface("");
                    }

                    valueMetaInterface.setName("args0");
                    list.add(valueMetaInterface);
                    outputMeta.setBasicTypeFlag(true);
                    outputMeta.setBasicFieldType(typeToFieldTypeCode(valueMetaInterface.getType()));
                }
                break;
            }
            outputMeta.setIsArray(true);

        } else if (tr instanceof Map) {
            outputMeta.setBasicTypeFlag(false);
//            outputMeta.setBasicFieldType(typeToFieldTypeCode(valueMetaInterface.getType()));
            outputMeta.setIsArray(false);
            ((Map) tr).forEach((kk, vv) -> {
                IValueMeta valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface(vv);
                if (ObjectUtil.isNull(valueMetaInterface)) {
                    valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface("");
                }
                if (valueMetaInterface instanceof ValueMetaArray) {
                    ProcessRowDescriptor openSkyRowMeta = objectToRowMeta(vv);
                    ((ValueMetaArray) valueMetaInterface).setFields(openSkyRowMeta.getValueMetaList());
                } else if (valueMetaInterface instanceof ValueMetaObject) {
                    ProcessRowDescriptor openSkyRowMeta = objectToRowMeta(vv);
                    ((ValueMetaObject) valueMetaInterface).setFields(openSkyRowMeta.getValueMetaList());
                }
                valueMetaInterface.setName((String) kk);
                list.add(valueMetaInterface);
            });
        } else {
            IValueMeta valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface(tr);
            if (ObjectUtil.isNull(valueMetaInterface)) {
                valueMetaInterface = ProcessConfigFactory.guessValueMetaInterface("");
            }
            valueMetaInterface.setName("args0");
            list.add(valueMetaInterface);
            outputMeta.setBasicTypeFlag(true);
            outputMeta.setBasicFieldType(typeToFieldTypeCode(valueMetaInterface.getType()));

        }
        outputMeta.setValueMetaList(list);
        return outputMeta;
    }

}
